<?php
class Controller_Offermanager extends Controller_Abstract
{
	public function actionIndex() {
		$emailOffersModel = new EmailOffers();
		if($this->_context->isPOST() && $this->_context->isAJAX()){
			$offerEmailSwitch = strtolower($emailOffersModel->getOfferEmailSwitch());
			$newState = ($offerEmailSwitch == 'on')? 'off' : 'on';
			$emailOffersModel->setEmailSwitch($newState);
			
			return ($newState == 'on')? 'Stop' : 'Run';
		}
		$this->_view['offerEmailSwitch'] = strtolower($emailOffersModel->getOfferEmailSwitch());
		$this->_view['eofsub1'] = $emailOffersModel->getOfferEmailSubject(1);
		$this->_view['eofsub2'] = $emailOffersModel->getOfferEmailSubject(2);
		$this->_view['eofsub3'] = $emailOffersModel->getOfferEmailSubject(3);
		if (isset($_SESSION['infos'])) {
			$this->_view['infos'] = $_SESSION['infos'];
			unset($_SESSION['infos']);
		}
		
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		$this->_view['emailOffers'] = $emailOffersModel->getAllEmailOffers();
	}
	
	public function actionUpdate() {
		$emailOfferModel = new EmailOffers();
		if ($this->_context->isGET() && isset($this->_context->id)) {
			$emailOffer = $emailOfferModel->getEmailOfferById($this->_context->id);
			if ($emailOffer) {
				$this->_view['emailOffer'] = $emailOffer;
				$this->_view['sites'] = DataUtil::getOptionsForSites();
			}
		} else {
			$id = $this->_context->post('id');
			$name = $this->_context->post('name', '');
			$subject = $this->_context->post('subject', '');
			$offerName = $this->_context->post('offer_name', '');
			$offerId = $this->_context->post('offer_id', '');
			$pictureUrl = $this->_context->post('picture_url', '');
			$pictureWidth = $this->_context->post('picture_width', '');
			$pictureHeight = $this->_context->post('picture_height', '');
			$linkUrl = $this->_context->post('link_url', '');
			$order = $this->_context->post('order', 1);
			$tosent_date = $this->_context->post('tosent_date');
			$siteId = $this->_context->post('site_id');
			$gender = $this->_context->post('gender');
			$errors = array();
			if (!filter_var($pictureUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(13004);
			}
			if (!filter_var($linkUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(13005);
			}
			if (count($errors) > 0) {
				$this->_view['errors'] = $errors;
				$this->_view['emailOffer']['id'] = $id;
				$this->_view['emailOffer']['name'] = $name;
				$this->_view['emailOffer']['subject'] = $subject;
				$this->_view['emailOffer']['offer_name'] = $offerName;
				$this->_view['emailOffer']['offer_id'] = $offerId;
				$this->_view['emailOffer']['picture_url'] = $pictureUrl;
				$this->_view['emailOffer']['picture_width'] = $pictureWidth;
				$this->_view['emailOffer']['picture_height'] = $pictureHeight;
				$this->_view['emailOffer']['link_url'] = $linkUrl;
				$this->_view['emailOffer']['ord'] = $order;
				$this->_view['emailOffer']['site_id'] = $siteId;
				$this->_view['emailOffer']['gender'] = $gender;
			} else {
				if ($emailOfferModel->updateEmailOffer($name, $subject, $offerName, $offerId, $pictureUrl, $pictureWidth, $pictureHeight, $linkUrl, $siteId, $order, $tosent_date, $gender, $id)) {
					$infos = array();
					$infos[] = Util::getMessage(13002);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('offermanager/index'));
				}
			}
		}
	}
	
	public function actionDelete() {
		$emailOffersModel = new EmailOffers();
		$emailOffersModel->deleteEmailOffer($this->_context->id);
		$infos = array();
		$infos[] = Util::getMessage(13003);
		$_SESSION['infos'] = $infos;
		return $this->_redirect(url('offermanager/index'));
	}
	
	public function actionAdd() {
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		if ($this->_context->isPOST()) {
			$name = $this->_context->post('name', '');
			$subject = $this->_context->post('subject', '');
			$offerName = $this->_context->post('offer_name', '');
			$offerId = $this->_context->post('offer_id', '');
			$pictureUrl = $this->_context->post('picture_url', '');
			$pictureWidth = $this->_context->post('picture_width', '');
			$pictureHeight = $this->_context->post('picture_height', '');
			$linkUrl = $this->_context->post('link_url', '');
			$order = $this->_context->post('order', 1);
			$tosent_date = $this->_context->post('tosent_date');
			$siteId = $this->_context->post('site_id');
			$gender = $this->_context->post('gender');
			if (!filter_var($pictureUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(13004);
			}
			if (!filter_var($linkUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(13005);
			}
			if (count($errors) > 0) {
				$this->_view['errors'] = $errors;
				$this->_view['offer_name']  = $offerName;
				$this->_view['offer_id'] = $offerId;
				$this->_view['name'] = $name;
				$this->_view['subject'] = $subject;
				$this->_view['picture_url'] = $pictureUrl;
				$this->_view['picture_width'] = $pictureWidth;
				$this->_view['picture_height'] = $pictureHeight;
				$this->_view['link_url'] = $linkUrl;
				$this->_view['order'] = $order;
				$this->_view['site_id'] = $siteId;
				$this->_view['tosent_date'] = $tosent_date;
				$this->_view['emailOffer']['gender'] = $gender;
			} else {
				$emailOffersModel = new EmailOffers();
				if ($emailOffersModel->addEmailOffer($name, $subject, $offerName, $offerId, $pictureUrl, $pictureWidth, $pictureHeight, $linkUrl, $siteId, $order, $tosent_date, $gender)) {
					$infos = array();
					$infos[] = Util::getMessage(13001);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('offermanager/index'));
				}
			}
		}
	}
	
	public function actionSetSubject()
	{
		if($this->_context->isPOST()){
			$newSubject_1 = $this->_context->post('emailOffersSubject_1');
			$newSubject_2 = $this->_context->post('emailOffersSubject_2');
			$newSubject_3 = $this->_context->post('emailOffersSubject_3');
			
			$emailOffersModel = new EmailOffers();
			$emailOffersModel->setOfferEmailSubject($newSubject_1, 1);
			$emailOffersModel->setOfferEmailSubject($newSubject_2, 2);
			$emailOffersModel->setOfferEmailSubject($newSubject_3, 3);			
		}
		
		return $this->_redirect(url('offermanager/index'));
	}
}